//
//  JQFYCreatGroupInfoTableViewCell.m
//  JQFY
//
//  Created by jack on 2017/10/9.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYCreatGroupInfoTableViewCell.h"

@interface JQFYCreatGroupInfoTableViewCell ()<UITextFieldDelegate>

@property (nonatomic, strong)UIButton *button;
@property (nonatomic, strong)UITextField *textField;

@end

@implementation JQFYCreatGroupInfoTableViewCell

#pragma mark - setter/getter

- (void)setCreatGroupModel:(JQFYCreatGroupModel *)creatGroupModel {
    if (_creatGroupModel == creatGroupModel) {
        return;
    }
    _creatGroupModel = creatGroupModel;
    [_button setImage:creatGroupModel.coverImage forState:UIControlStateNormal];
    
}

#pragma mark - action

- (void)tapAction:(UIButton *)button {
//    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedCoverImage)]) {
//        [self.delegate didSelectedCoverImage];
//    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedCoverImage:)]) {
        [self.delegate didSelectedCoverImage:button];
    }
}

#pragma mark - override

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_button];
    _button.layer.cornerRadius = 10;
    _button.layer.masksToBounds = YES;
    [_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(30 *SCAL);
        make.size.mas_equalTo(132 *SCAL);
    }];
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.font = [UIFont systemFontOfSize:12 *SCAL];
    tipLabel.numberOfLines = 2;
    tipLabel.text = @"请点击以上图片\n上传你的群聊的封面";
    tipLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:tipLabel];
    
    _textField = [[UITextField alloc] init];
    _textField.delegate = self;
    _textField.placeholder = @"请点击此处输入你的群聊标题";
    _textField.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_textField];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_button.mas_bottom).offset(6 *SCAL);
        make.centerX.equalTo(self.contentView);
    }];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(35 *SCAL);
        make.trailing.equalTo(self.contentView).offset(-35 *SCAL);
        make.top.equalTo(tipLabel.mas_bottom).offset(17 *SCAL);
        make.height.mas_equalTo(33 *SCAL);
    }];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    self.creatGroupModel.groupName = textField.text;
    return YES;
}

+ (CGFloat)heigtForCell {
    CGFloat height = 0;
    height += 30 *SCAL;
    height += 132 *SCAL;
    height += 6 *SCAL;
    height += 29 *SCAL;
    height += 17 *SCAL;
    height += 33 *SCAL;
    return height;
}

@end
